//
//  UIImageView+.swift
//  shanshanlol微博
//
//  Created by 叶竞泽 on 2017/1/23.
//  Copyright © 2017年 JingZe. All rights reserved.
//

import SDWebImage

extension UIImageView {
    
    /// 包装sd_webImage设置图像函数
    ///
    /// - Parameters:
    ///   - urlString: urlString
    ///   - placeholderImage: 占位图
    ///   - isAvatar: 是否是头像
    func cz_setImage(urlString: String, placeholderImage: UIImage?, isAvatar: Bool = false) {
        
        /// 处理url
        guard let url = URL(string: urlString) else {
            // 设置占位图
            image = placeholderImage
            return
        }
        
        /// 可选项只是用在swift,oc用!同样可以传入nil
        sd_setImage(with: url, placeholderImage: placeholderImage, options: [], progress: nil) { [weak self] (image, _, _, _) in
            
            // 完成回调,判断是否是头像
            if isAvatar {
                
                self?.image = image?.cz_avatarImage(size: self?.bounds.size)
            }
        }
    }
    
}
